var ALY = require('aliyun-sdk');
var ossStream = require('aliyun-oss-upload-stream')(new ALY.OSS({
    accessKeyId: 'LTAIshyP8Q5XNPw9',
    secretAccessKey: 'jTq3LnlrDlSdNRQzmN1jFBvCfU7v8E',
    endpoint: 'http://oss-cn-shanghai.aliyuncs.com',
    apiVersion: '2013-10-15'
}));

module.exports = {
    upload:function (fileStream, filename, cb) {

        if (!fileStream || !filename){
           cb('upload failed:invalid parameter');
        }

        var upload = ossStream.upload({
            Bucket: 'smtoolfile',
            Key: '2017/'+filename
        });

        upload.on('error', function (error) {
            cb(error);
        });

        upload.on('part', function (part) {

        });

        upload.on('uploaded', function (details) {
            fileStream.unpipe(upload);
            cb(null, details);
        });

        fileStream.pipe(upload);
    }
}
